<?

	////
	///
	//	Organization

		function SO_organization( $organization, $account = null ) {

			$query = "SELECT account, `JC_FK_account.name`( `account` ) AS `account FKF` FROM `organization` WHERE `key` = '" . $organization . "'";

			$recordSet = JO_query( $query );

			if( $recordSet -> num_rows )	$record = $recordSet -> fetch_array( );
			else							return false;

			$result = JO_textArray( $record[ "account FKF" ], ":", "\r\n" );

			if( empty( $account ) )	return $result;
			else					return array_key_exists( $account, $result );

		}



	////
	///
	//	Index

		function SO_index_insert( ) {

			$query = "
				INSERT INTO `index` (
					`unit`		,
					`subject`	,
					`content`	,
					`mark`		,
					`image`		,
					`link`		,
					`log`		,
					`insert`
				) VALUES (
					" . (	$_REQUEST[ "SO_index_unit"		] ? "'" . $_REQUEST[ "SO_index_unit"	] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_index_subject"	] ? "'" . $_REQUEST[ "SO_index_subject"	] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_index_content"	] ? "'" . $_REQUEST[ "SO_index_content"	] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_index_mark"		] ? "'" . $_REQUEST[ "SO_index_mark"	] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_index_image"		] ? "'" . $_REQUEST[ "SO_index_image"	] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_index_link"		] ? "'" . $_REQUEST[ "SO_index_link"	] . "'" : "NULL" ) . ",
					CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					NOW( )
				)
			";

			JO_query( $query );

			JO_location( );

		}

		function SO_index_update( ) {

			$query = "
				UPDATE `index` SET
					`unit`		= " . (	$_REQUEST[ "SO_index_unit"		] ? "'" . $_REQUEST[ "SO_index_unit"	] . "'" : "NULL" ) . ",
					`subject`	= " . (	$_REQUEST[ "SO_index_subject"	] ? "'" . $_REQUEST[ "SO_index_subject"	] . "'" : "NULL" ) . ",
					`content`	= " . (	$_REQUEST[ "SO_index_content"	] ? "'" . $_REQUEST[ "SO_index_content"	] . "'" : "NULL" ) . ",
					`mark`		= " . (	$_REQUEST[ "SO_index_mark"		] ? "'" . $_REQUEST[ "SO_index_mark"	] . "'" : "NULL" ) . ",
					`image`		= " . (	$_REQUEST[ "SO_index_image"		] ? "'" . $_REQUEST[ "SO_index_image"	] . "'" : "NULL" ) . ",
					`link`		= " . (	$_REQUEST[ "SO_index_link"		] ? "'" . $_REQUEST[ "SO_index_link"	] . "'" : "NULL" ) . ",
					`log`		= CONCAT( `log`, NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					`insert`	= '" . $_REQUEST[ "SO_index_insert"		] . "',
					`delete`	= " . (	$_REQUEST[ "SO_index_delete"	] ? "'" . $_REQUEST[ "SO_index_delete"	] . "'" : "NULL" ) . ",
					`update`	= NOW( )
				WHERE `key`		= '" .	$_REQUEST[ "SO_index_key"		] . "'			
			";

			JO_query( $query );

			JO_location( );

		}



	////
	///
	//	Content

		function SO_content_insert( ) {

			$query = "
				INSERT INTO `content` (
					`unit`		,
					`subject`	,
					`content`	,
					`image`		,
					`log`		,
					`insert`
				) VALUES (
					" . (	$_REQUEST[ "SO_content_unit"	] ? "'" . $_REQUEST[ "SO_content_unit"	] . "'" : "NULL" ) . ",
					'" .	$_REQUEST[ "SO_content_subject"	] . "',
					'" .	$_REQUEST[ "SO_content_content"	] . "',
					" . (	$_REQUEST[ "SO_content_image"	] ? "'" . $_REQUEST[ "SO_content_image"	] . "'" : "NULL" ) . ",
					CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					NOW( )
				)
			";

			JO_query( $query );

			JO_location( );

		}

		function SO_content_update( ) {

			$query = "
				UPDATE `content` SET
					`unit`		= " . (	$_REQUEST[ "SO_content_unit"	] ? "'" . $_REQUEST[ "SO_content_unit"	] . "'" : "NULL" ) . ",
					`subject`	= '" .	$_REQUEST[ "SO_content_subject"	] . "',
					`content`	= '" .	$_REQUEST[ "SO_content_content"	] . "',
					`image`		= " . (	$_REQUEST[ "SO_content_image"	] ? "'" . $_REQUEST[ "SO_content_image"	] . "'" : "NULL" ) . ",
					`log`		= CONCAT( `log`, NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					`insert`	= '" . $_REQUEST[ "SO_index_insert"		] . "',
					`delete`	= " . (	$_REQUEST[ "SO_index_delete"	] ? "'" . $_REQUEST[ "SO_index_delete"	] . "'" : "NULL" ) . ",
					`update`	= NOW( )
				WHERE `key`		= '" .	$_REQUEST[ "SO_content_key"		] . "'			
			";

			JO_query( $query );

			JO_location( );

		}



	////
	///
	//	Task

		function SO_task_insert( ) {

			$query = "
				INSERT INTO `task` (
					`parent`	,
					`priority`	,
					`manager`	,
					`operator`	,
					`type`		,
					`subject`	,
					`content`	,
					`link`		,
					`period`	,
					`progress`	,
					`log`		,
					`insert`
				) VALUES (
					" . (	$_REQUEST[ "SO_task_parent"		] ? "'" . $_REQUEST[ "SO_task_parent"	] . "'" : "NULL" ) . ",
					'" .	$_REQUEST[ "SO_task_priority"	] . "',
					'" .	$_SESSION[ "JO_account_key"		] . "',
					'" .	$_REQUEST[ "SO_task_operator"	] . "',
					'" .	$_REQUEST[ "SO_task_type"		] . "',
					'" .	$_REQUEST[ "SO_task_subject"	] . "',
					" . (	$_REQUEST[ "SO_task_content"	] ? "'" . $_REQUEST[ "SO_task_content"	] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_task_link"		] ? "'" . $_REQUEST[ "SO_task_link"		] . "'" : "NULL" ) . ",
					" . (	$_REQUEST[ "SO_task_period"		] ? "'" . $_REQUEST[ "SO_task_period"	] . "'" : "NULL" ) . ",
					'" .	$_REQUEST[ "SO_task_progress"	] . "',
					CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					NOW( )
				)
			";

			JO_query( $query );

			JO_location( );

		}

		function SO_task_update( ) {

			$query = "
				UPDATE `task` SET
					`parent`	= " . (	$_REQUEST[ "SO_task_parent"		] ? "'" . $_REQUEST[ "SO_task_parent"	] . "'" : "NULL" ) . ",
					`priority`	= '" .	$_REQUEST[ "SO_task_priority"	] . "',
					`manager`	= '" .	$_SESSION[ "JO_account_key"		] . "',
					`operator`	= '" .	$_REQUEST[ "SO_task_operator"	] . "',
					`type`		= '" .	$_REQUEST[ "SO_task_type"		] . "',
					`subject`	= '" .	$_REQUEST[ "SO_task_subject"	] . "',
					`content`	= " . (	$_REQUEST[ "SO_task_content"	] ? "'" . $_REQUEST[ "SO_task_content"	] . "'" : "NULL" ) . ",
					`link`		= " . (	$_REQUEST[ "SO_task_link"		] ? "'" . $_REQUEST[ "SO_task_link"		] . "'" : "NULL" ) . ",
					`period`	= " . (	$_REQUEST[ "SO_task_period"		] ? "'" . $_REQUEST[ "SO_task_period"	] . "'" : "NULL" ) . ",
					`progress`	= '" .	$_REQUEST[ "SO_task_progress"	] . "',
					`log`		= CONCAT( `log`, NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					`update`	= NOW( )
				WHERE `key`		= '" .	$_REQUEST[ "SO_task_key"		] . "'			
			";

			JO_query( $query );

			JO_location( );

		}

		function SO_task_report( ) {

			$query = "
				INSERT INTO `report` (
					`account`	,
					`task`		,
					`content`	,
					`log`		,
					`insert`
				) VALUES (
					'" . $_SESSION[ "JO_account_key"	] . "',
					'" . $_REQUEST[ "SO_task_key"		] . "',
					'" . $_REQUEST[ "SO_task_report"	] . "',
					CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					NOW( )
				)
			";

			JO_query( $query );

			JO_location( );

		}
		
	////
	///
	//	Upload image
		function SO_Upload_Image($SO_file,$pathfile){
			$today = gmdate("YmdHis",time()+(7*3600));
			if ((($_FILES[$SO_file]["type"] == "image/gif")
			|| ($_FILES[$SO_file]["type"] == "image/jpeg")
			|| ($_FILES[$SO_file]["type"] == "image/pjpeg"))
			&& ($_FILES[$SO_file]["size"] < 2000000))//2,000,000:2MB
			{
			  if ($_FILES[$SO_file]["error"] > 0)
			  	return 1;
			  else{	
			    if (file_exists("../".$pathfile."/".$_FILES[$SO_file]["name"]))
			    	return 2;
			    else{
			      move_uploaded_file($_FILES[$SO_file]["tmp_name"],"../".$pathfile."/".$today."_".$_FILES[$SO_file]["name"]);
			      return $today."_".$_FILES[$SO_file]["name"];
			    }
			  }
			}
			else
			  return 3;
		}
		
		
	////
	///
	//	Show
		function SO_show_insert(){
			
			//upload image to server
			$sImage = NULL;
			if(isset($_POST["txt_image"])){
				$sImage = $_POST["txt_image"];
			}
			else{
				$sResult = SO_Upload_Image("ub_file","JO_84_image_show");
				
				if($sResult=="3")
					exit("<script>alert( 'invalid file type.' );</script>");
				elseif ($sResult=="1")
					exit("<script>alert( 'Can not copy file to server.' );</script>");
				elseif ($sResult=="2")
					exit("<script>alert( 'File already exists.' );</script>");
				else 
					$sImage = "JO_84_image_show/".$sResult;
			}
			//end upload image
			
			$query = "
				INSERT INTO `show` (
					`subject`		,
					`channel`		,
					`description`	,
					`episode`		,
					`image`			,
					`log`			,
					`insert`
				) VALUES (
					" . ( $_POST[ "txt_subject"		] ? "'" . $_REQUEST[ "txt_subject"		] . "'" : "NULL" ) . ",
					" . ( $_POST[ "cbo_channel"		] ? "'" . $_REQUEST[ "cbo_channel"		] . "'" : "NULL" ) . ",
					" . ( $_POST[ "ta_description"	] ? "'" . $_REQUEST[ "ta_description"	] . "'" : "NULL" ) . ",
					" . ( $_POST[ "txt_episode"		] ? "'" . $_REQUEST[ "txt_episode"		] . "'" : "NULL" ) . ",
					" . ( $sImage != NULL			  ? "'" . $sImage 						  . "'" : "NULL" ) . ",
					CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					NOW( )
				)
			";
			
			JO_query( $query );
			
			$query = "select last_insert_id() as show_id";
			$recordSet=JO_query( $query );
			$record= $recordSet-> fetch_array();
			SO_insert_show_detail($record["show_id"]);

			JO_location();
		}
		
		function SO_show_update(){
			
			//upload image to server
			$sImage = NULL;
			if(isset($_POST["txt_image"])){
				$sImage = $_POST["txt_image"];
			}
			else{
				$sResult = SO_Upload_Image("ub_file","JO_84_image_show");
				
				if($sResult=="3")
					exit("<script>alert( 'invalid file type.' );</script>");
				elseif ($sResult=="1")
					exit("<script>alert( 'Can not copy file to server.' );</script>");
				elseif ($sResult=="2")
					exit("<script>alert( 'File already exists.' );</script>");
				else 
					$sImage = "JO_84_image_show/".$sResult;
			}
			//end upload image
			
			$query = "
				UPDATE `show` SET
					`subject`		= '" . $_POST[ "txt_subject"	] . "',
					`channel`		= '" . $_POST[ "cbo_channel"	] . "',
					`description`	= '" . $_POST[ "ta_description"	] . "',
					`episode`		= '" . $_POST[ "txt_episode"	] . "',
					`image`			=  " . ( $sImage != NULL ? "'".$sImage."'" : "NULL" ). ",
					`log`			= CONCAT( `log`, NOW( ), ' " . $_SESSION[ "JO_account_key" ] . " \r\n' ),
					`update`		= NOW( )
				WHERE `key`			= '" . $_REQUEST[ "SO_show_key" ] . "'	
			";

			echo $query;
			JO_query( $query );
			
			SO_delete_show_detail($_REQUEST[ "SO_show_key" ]);
			SO_insert_show_detail($_REQUEST[ "SO_show_key" ]);
			
			JO_location();
		}
		
		function SO_show_delete(){
			$query = "UPDATE `show` SET
	 			`delete` = NOW( )
	 			WHERE " .	$_REQUEST[ "wherequery" ] . "
			";
	 		
			JO_query( $query );
	 		JO_location( );
		}
		
	////
	///
	//	Show detail
	
		function SO_delete_show_detail($i_show_ID){
			$query = "
				DELETE FROM show_detail 
				WHERE `show` =".$i_show_ID;
			JO_query( $query );
		}
		
		function SO_insert_show_detail($i_show_ID){
			
			if($_POST["SO_show_array"]>0){
								
				$s_value = "VALUES ";
				for ($i = 0; $i <$_POST["SO_show_array"]; $i++) {
					if($_POST["txt_part".$i]!=NULL && $_POST["txt_url".$i]!=NULL){
						$s_value .= "
							(
								". $i_show_ID .",
								".($_POST["txt_url".$i]	? "'".htmlentities($_POST["txt_url".$i	]).		"'" : "NULL"	).",
								".($_POST["txt_mota".$i]? "'".htmlentities($_POST["txt_mota".$i ],ENT_QUOTES).	"'" : "NULL"	).",
								".($_POST["txt_part".$i]? "'".$_POST["txt_part".$i	].				"'" : "NULL"	).",
								CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
								NOW()
							),";
					}
				}
				$query = "
					INSERT INTO `show_detail` (
						`show`			,
						`url`			,
						`description`	,
						`part`			,
						`log`			,
						`insert`
					) ".substr($s_value, 0, -1).";
				";
				echo $i_show_ID;
				JO_query( $query );
			}
			
		}
		
		//---------------------------------------------------------Quan----------------------------------------
		$sub=$_POST['SO_category_subject'];
		$chan=$_POST['SO_category_channel'];
		$img=$_POST['SO_category_images'];
		$bro=$_POST['SO_category_broadcast'];
		$rebro=$_POST['SO_category_rebroadcast'];
		$insert=$_POST['SO_category_insert'];
		$delete=$_POST['SO_category_delete'];
		$key=$_POST['SO_category_key'];
		
	
		function SO_show_category_insert()
		{
		
			if($_POST["SO_category_checked"]!='')
			{
			$tenhinh = "../JO_84_image_show/".$_FILES['SO_category_images']['name'];
			}
			else 
			{
			$tenhinh=$_POST['SO_category_images'];
			}
			$Query=(
			 "INSERT INTO `show_category` (
						`subject` ,
						`channel` ,
						`image` ,
						`broadcast` ,
						`rebroadcast` ,
						`log`,
						`insert`
					) VALUES (
					" . (	$_POST['SO_category_subject'] ? "'" . $_POST['SO_category_subject'] . "'" : "NULL" ) . ",
					" . (	$_POST['SO_category_channel'] ? "'" . $_POST['SO_category_channel'] . "'" : "NULL" ) . ",
					" . (	$tenhinh ? "'" . $tenhinh . "'" : "NULL" ) . ",
					" . (	$_POST['SO_category_broadcast'] ? "'" . $_POST['SO_category_broadcast'] . "'" : "NULL" ) . ",
					" . (	$_POST['SO_category_rebroadcast'] ? "'" . $_POST['SO_category_rebroadcast'] . "'" : "NULL" ) . ",
						CONCAT( NOW( ), ' " . $_SESSION[ "JO_account_key" 	] . "\r\n' ),
						NOW( )
					)
				");
			JO_query( $Query );
			JO_location( );

		}
		function SO_show_category_update()
		{
		if($_POST["SO_category_checked"]!='')
		{
		$tenhinh = "../JO_84_image_show/".$_FILES['SO_category_images']['name'];
		}
		else 
		{
		$tenhinh=$_POST['SO_category_images'];
		}

			$Query=(
			"
				UPDATE `show_category` SET
					`subject`		= " . (	$_POST[ "SO_category_subject"	] ? "'" . $_POST[ "SO_category_subject"	] . "'" : "NULL" ) . ",
					`channel`		= " . (	$_POST[ "SO_category_channel"	] ? "'" . $_POST[ "SO_category_channel"	] . "'" : "NULL" ) . ",
					`image`			= " . (	$tenhinh ? "'" . $tenhinh . "'" : "NULL" ) . ",
					`broadcast`		= " . (	$_POST[ "SO_category_broadcast"	] ? "'" . $_POST[ "SO_category_broadcast"	] . "'" : "NULL" ) . ",
					`rebroadcast`	= " . (	$_POST[ "SO_category_rebroadcast"] ? "'" . $_POST[ "SO_category_rebroadcast"	] . "'" : "NULL" ) . ",
					
					`log`		= CONCAT( `log`, NOW( ), ' " . $_SESSION[ "JO_account_key" ] . "\r\n' ),
					`insert`	= '" . $_POST[ "SO_category_insert"			] . "',
					`delete`	= NULL,
					`update`	= NOW( )
				WHERE `key`		= '" .	$_POST['SO_category_key'				] . "'		
			");	
			JO_query( $Query );
			JO_location( );
			
		}
		
		function SO_show_category_delete()
		{
			foreach($_POST["chb"]  as $t=>$gt)
 			{
				$day=date('j-m-y,H:m:s ');
	 			$Query=("UPDATE `jo`.`show_category` SET `delete` = '$day' WHERE `show_category`.`key` =$gt");
				JO_query( $Query );
		 	}
			
			JO_location( );
		}
		//---------------------------------------------------end Quan -------------------------------------------
?>
